// pages/sudoku/sudoku.js
import bank from './bank'
Page({

    data: {
        currentAxis: null,
        time: '00:00',
        rankItem: {}
    },

    onLoad(options) {
        this.setGridSize()
        this.getRankItem()
        this.getDate()
        this.onTimer()
    },

    getRankItem() {
        this.setData({
            rankItem: bank[0]
        })
    },

    onTapGridItem(e) {
        const axis = e.currentTarget.dataset.axis
        console.log(axis);
        this.setData({
            currentAxis: axis
        })
        this.onQuerySameValue(axis)
    },

    onQuerySameValue(axis) {
        const item = this.data.rankItem.outerList[axis[0]].innerList[axis[1]]
        const value = item.fixed ? item.answer : item.value
        this.data.rankItem.outerList.forEach(outer => {
            outer.innerList.forEach(inner => {
                this.setData({
                    [`rankItem.outerList[${outer.index}].innerList[${inner.index}].same`]: false
                })
                if (!value) {
                    return
                }
                if (inner.fixed) {
                    inner.same = inner.answer === value
                } else {
                    inner.same = inner.value === value
                }
                if (inner.same) {
                    this.setData({
                        [`rankItem.outerList[${outer.index}].innerList[${inner.index}].same`]: true
                    })
                }
            })
        })
    },

    onCheck(axis) {
        const item = this.data.rankItem.outerList[axis[0]].innerList[axis[1]]
        if (item.fixed) { return }
        if (item.value === item.answer) {
            this.setData({
                [`rankItem.outerList[${axis[0]}].innerList[${axis[1]}].error`]: false
            })
        } else {
            wx.vibrateShort({
                type: 'light'
            })
            this.setData({
                [`rankItem.outerList[${axis[0]}].innerList[${axis[1]}].error`]: true
            })
        }
    },

    onTapOption(e) {
        const axis = this.data.currentAxis
        if (!axis) { return }
        const option = e.currentTarget.dataset.index + 1
        this.setData({
            [`rankItem.outerList[${axis[0]}].innerList[${axis[1]}].value`]: option
        })
        this.onCheck(axis)
        // this.onQuerySameValue(axis)
    },

    onTimer() {
        const startStamp = this.data.startStamp

        const interval = setInterval(() => {
            const currentStamp = new Date().getTime()
            const res = currentStamp - startStamp
            let minutes = parseInt((res % (1000 * 60 * 60)) / (1000 * 60))
            minutes = minutes < 10 ? '0' + minutes : minutes
            let seconds = ((res % (1000 * 60)) / 1000).toFixed(0)
            seconds = seconds < 10 ? '0' + seconds : seconds
            this.setData({
                time: `${minutes}:${seconds}`
            })
        }, 1000)
    },

    getDate() {
        const date = new Date()
        let startStamp = date.getTime()
        this.setData({
            startStamp,
            date: `${date.getMonth() + 1}月${date.getDate()}日`
        })
    },

    setGridSize() {
        const windowWidth = wx.getSystemInfoSync().windowWidth
        this.setData({
            windowWidth
        })
    },

    onShareAppMessage: function () {

    }
})